package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.BehaviorService;
import com.heima.common.constants.HotArticleConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.LikesBehaviorDto;
import com.heima.model.behavior.ReadBehaviorDto;
import com.heima.model.behavior.UpdateArticleMess;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import thread.AppThreadLocalUtil;

@Transactional
@Service
public class BehaviorServiceImpl implements BehaviorService {
    @Autowired
    private CacheService cacheService;

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    /**
     * 文章点赞
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        //operation 0点赞 1取消   type 0文章 1动态 2评论
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取当前登录用户
        ApUser apUser = AppThreadLocalUtil.getApUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //更新当次点赞数据,实时发送
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
        mess.setArticleId(dto.getArticleId());
        //保存数据
        if (dto.getOperation() == 0) {
            //点赞 ,保存到redis
            cacheService.hPut("BEHAVIORLIKES_" + dto.getArticleId(), apUser.getId().toString(), JSON.toJSONString(dto));
            mess.setAdd(1);
        } else {
            //取消点赞,删除redis
            cacheService.hDelete("BEHAVIORLIKES_" + dto.getArticleId(), apUser.getId().toString());
            mess.setAdd(-1);
        }
        //kafka发送消息
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, JSON.toJSONString(mess));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 阅读
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult read(ReadBehaviorDto dto) {
        //参数校验
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取当前登录用户
        ApUser apUser = AppThreadLocalUtil.getApUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //更新次数
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setType(UpdateArticleMess.UpdateArticleType.VIEWS);
        mess.setArticleId(dto.getArticleId());
        //判断当前用户当前文章阅读次数
        String readBehavior = (String) cacheService.hGet("BEHAVIORREAD_" + dto.getArticleId(), apUser.getId().toString());
        if (StringUtils.isNotBlank(readBehavior)) {
            //增加次数,根据键获取之前数据
            ReadBehaviorDto readBehaviorDto = JSON.parseObject(readBehavior, ReadBehaviorDto.class);
            dto.setCount((short) (readBehaviorDto.getCount() + dto.getCount()));
        }
        //保存key
        cacheService.hPut("BEHAVIORREAD_" + dto.getArticleId(), apUser.getId().toString(), JSON.toJSONString(dto));
        mess.setAdd(1);
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, JSON.toJSONString(mess));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 不喜欢 0  取消不喜欢1
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult un_likes(LikesBehaviorDto dto) {
        //参数校验
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取当前登录用户
        ApUser apUser = AppThreadLocalUtil.getApUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //保存数据 不喜欢 0  取消不喜欢1
        if (dto.getType() == 0) {
            //添加数据
            cacheService.hPut("BEHAVIORUNLIKES_" + dto.getArticleId(), apUser.getId().toString(), JSON.toJSONString(dto));
        } else {
            //删除数据
            cacheService.hDelete("BEHAVIORUNLIKES_" + dto.getArticleId(), apUser.getId().toString());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
